package com.shujia.mr.hw.home3;

import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class Student implements WritableComparable<Student> {
    String studentID;
    String name;
    String age;
    String gender;
    String clazz;
    String totalScore;


    public Student() {
    }

    public Student(String studentID, String name, String age, String gender, String clazz, String totalScore) {
        this.studentID = studentID;
        this.name = name;
        this.age = age;
        this.gender = gender;
        this.clazz = clazz;
        this.totalScore = totalScore;
    }

    @Override
    public String toString() {
        return studentID + ',' + name + ',' + age + ',' + gender + ',' +
                clazz + ',' +
                totalScore ;
    }

    public String getStudentID() {
        return studentID;
    }

    public void setStudentID(String studentID) {
        this.studentID = studentID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getClazz() {
        return clazz;
    }

    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    public String getTotalScore() {
        return totalScore;
    }

    public void setTotalScore(String totalScore) {
        this.totalScore = totalScore;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(studentID);
        out.writeUTF(name);
        out.writeUTF(age);
        out.writeUTF(gender);
        out.writeUTF(clazz);
        out.writeUTF(totalScore);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        studentID = in.readUTF();
        name = in.readUTF();
        age = in.readUTF();
        gender = in.readUTF();
        clazz = in.readUTF();
        totalScore = in.readUTF();
    }

    // 自定义比较函数，实现倒叙排序
    @Override
    public int compareTo(Student other) {
        int thisValue = Integer.parseInt(this.totalScore);
        int thatValue = Integer.parseInt(other.totalScore);
        return -(thisValue < thatValue ? -1 : (thisValue == thatValue ? 0 : 1));
    }
}
